﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Chenxin.Lib.Utility;

namespace Chenxin.Lib.Hr.BLL
{
    public class AnnnualVacation
    {
        
        //*******************************************
        //功能概要       ：得到年假总天数
        //参数説明       ：UserId 用户ID
        //返回值説明     ：无
        //功能詳細       ：得到年假总天数
        //作成者         ：he.jifeng 
        //作成日         ：2010-03-26
        //*******************************************
        public Decimal getTotalCntOfAnnnualVac(string UserId)
        {
            return Chenxin.Lib.Hr.DAL.AnnnualVacation.getTotalCntOfAnnnualVac(UserId);
        }

        //*******************************************
        //功能概要       ：得到已用年假天数
        //参数説明       ：UserId 用户ID
        //返回值説明     ：无
        //功能詳細       ：得到已用年假天数
        //作成者         ：he.jifeng 
        //作成日         ：2010-03-26
        //*******************************************
        public Decimal getUsedCntOfAnnnualVac(string UserId, string RecordId)
        {
            return Chenxin.Lib.Hr.DAL.AnnnualVacation.getUsedCntOfAnnnualVac(UserId, RecordId);
        }

        //*******************************************
        //功能概要       ：得到年假使用情况一览
        //参数説明       ：hsParams 参数列表
        //返回值説明     ：无
        //功能詳細       ：得到年假使用情况一览
        //作成者         ：he.jifeng 
        //作成日         ：2010-03-26
        //*******************************************
        public DataSet getListOfAnnnualVac(System.Collections.Hashtable hsParams)
        {
            //select文
            string strSelect;
            //order文
            string strOrder = "";
            //当前页数
            int intCurPage = 1;
            //显示行数
            int intRowCnt = 10;
            //table名
            string strTableNm = " Hr_UserInfo ";
            //where文
            StringBuilder strWhere = new StringBuilder();
            strWhere.Append(" IsVirtual=0 ");
            strWhere.Append(" And Status = 1 ");
            strWhere.Append(" And Validate = 1 ");

            //权限
            string strPermit;

            //权限
            if (hsParams.ContainsKey("Permit"))
            {
                strPermit = hsParams["Permit"].ToString();
                if (strPermit == Constants.Permit_ViewAllAnnnualVac)
                {
                    //全部
                }
                else if (strPermit == Constants.Permit_ViewCityAnnnualVac)
                {
                    strWhere.AppendFormat(" And BelongCityId = '{0}'", hsParams["CurCityId"]);
                }
                else if (strPermit == Constants.Permit_ViewCompanyAnnnualVac)
                {
                    strWhere.AppendFormat(" And BelongCompanyId= '{0}'", hsParams["CurCompanyId"]);
                }
                else if (strPermit == Constants.Permit_ViewDepartAnnnualVac)
                {
                    strWhere.Append(" And EXISTS(");
                    strWhere.AppendFormat("Select 1 from Struct_UserDepartRelation M WHERE Hr_UserInfo.BelongDepartId=M.DepartId And M.Userid= '{0}' And M.ViewAnnualLeave=1 ", hsParams["CurUserId"]);
                    strWhere.Append(") ");
                }
            }
            //权限为当前用户
            else if (hsParams.ContainsKey("CurUserId"))
            {
                strWhere.Append(" And UserId = '" + hsParams["CurUserId"] + "'");
            }

            //用户名
            if (hsParams.ContainsKey("UserId"))
            {
                strWhere.Append(" And UserId = '" + hsParams["UserId"] + "'");
            }
            //部门
            else if (hsParams.ContainsKey("DepartId"))
            {
                strWhere.Append(" And BelongDepartId = '" + hsParams["DepartId"] + "'");
            }
            //商圈
            else if (hsParams.ContainsKey("AreaId"))
            {
                Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo entArea = null;
                string strCachkey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_DEPARTMENT, hsParams["AreaId"]);
                Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);

                entArea = cs.GetObject<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo>(strCachkey);
                if (!cs.Hit)
                {
                    entArea = Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetModel(new Guid(hsParams["AreaId"].ToString()));
                    if (entArea != null) cs.Set(strCachkey, entArea, DateTime.Now.AddDays(1));
                }

                if (entArea != null)
                {
                    strWhere.AppendFormat(" AND (BelongDepartId in (select departId from Hr_DepartmentInfo where DepartSrc like '{0}%'))", entArea.DepartSrc);
                }
            }
            //公司
            else if (hsParams.ContainsKey("CompanyId"))
            {
                strWhere.AppendFormat(" AND (BelongCompanyId = '{0}')", hsParams["CompanyId"]);
            }
            //城市
            else if (hsParams.ContainsKey("CityId"))
            {
                strWhere.AppendFormat(" AND ( BelongCityId = '{0}')", hsParams["CityId"]);
            }

            //排序项目
            if (hsParams.ContainsKey("sidx"))
            {
                strOrder = strOrder + hsParams["sidx"].ToString();
            }
            //排序方式
            if (hsParams.ContainsKey("sord"))
            {
                strOrder = strOrder + " " + hsParams["sord"].ToString();
            }

            //当前页数
            if (hsParams.ContainsKey("page"))
            {
                intCurPage = Convert.ToInt32(hsParams["page"]);
            }
            //显示行数
            if (hsParams.ContainsKey("rows"))
            {
                intRowCnt = Convert.ToInt32(hsParams["rows"]);
            }

            //strWhere = strWhere + " Group by Hr_UserInfo.UserId,Hr_UserInfo.UserName,Hr_UserInfo.DepartId,Hr_UserInfo.DepartName ";

            strSelect = " UserId,UserName,DepartId,DepartName,CntOfTotal,CntOfUsed,CntOfRemain ";

            string TableFieldList = "UserId,UserName,DepartId,DepartName,";
            TableFieldList = TableFieldList + "dbo.FN_GetTotalCntOfAnnnualVac(Hr_UserInfo.UserId) AS CntOfTotal,";
            TableFieldList = TableFieldList + "dbo.FN_GetUsedCntOfAnnnualVac(Hr_UserInfo.UserId) AS CntOfUsed,";
            TableFieldList = TableFieldList + "(dbo.FN_GetTotalCntOfAnnnualVac(Hr_UserInfo.UserId) - dbo.FN_GetUsedCntOfAnnnualVac(Hr_UserInfo.UserId)) AS CntOfRemain";

            //开始范围
            int intSelStart;
            //结束范围
            int intSelEnd;

            intSelStart = (intCurPage - 1) * intRowCnt + 1;
            intSelEnd = intCurPage * intRowCnt;
            DataSet ds = Chenxin.Lib.Hr.DAL.AnnnualVacation.getListOfAnnnualVac(strTableNm, strSelect, TableFieldList, strWhere.ToString(), strOrder, intCurPage, intRowCnt, true);

            return ds;
        }

         //*******************************************
        //功能概要       ：得到年假使用情况一览总件数
        //参数説明       ：hsParams 参数列表
        //返回值説明     ：无
        //功能詳細       ：得到年假使用情况一览总件数
        //作成者         ：he.jifeng 
        //作成日         ：2010-03-26
        //*******************************************
        public int getCntOfAnnnualVac(System.Collections.Hashtable hsParams)
        {
            
            //where文
            string strWhere = " 1=1 ";
            //用户ID
            if (hsParams.ContainsKey("UserId"))
            {
                strWhere = strWhere + " And UserId = '" + hsParams["UserId"].ToString() + "' ";
             
            }else if (hsParams.ContainsKey("DepartId"))
            {
                //部门
                strWhere = strWhere + " And DepartId = '" + hsParams["DepartId"].ToString() + "' ";
            }

            DataSet ds = Chenxin.Lib.Hr.DAL.AnnnualVacation.getListOfAnnnualVac("Hr_UserInfo", "", "", strWhere, "", 0, 0, false);

            //取得总记录数
            if (ds.Tables[0].Rows.Count > 0)
            {
                return Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            }
            else
            {
                return 0;
            }
        }
    }
}
